package data;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;


public class ProductList extends ListResponse {
	private List<ProductInfo> mResult = new ArrayList<>();
	
	public ProductList(int exitCode, ProductInfo[] products) {
		this.mExitCode = exitCode;
		if (products != null)
			for (ProductInfo productInfo : products)
				mResult.add(productInfo);
	}
	
	public ProductList(int exitCode, List<ProductInfo> products) {
		this.mExitCode = exitCode;
		if (products != null)
			for (ProductInfo productInfo : products)
				mResult.add(productInfo);
	}
	
	public List<ProductInfo> getList() {
		return mResult;
	}
	
	public final static JsonDeserializer<ProductList> deserializer = new JsonDeserializer<ProductList>() {

		@Override
		public ProductList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
				throws JsonParseException {
			
			int exitCode = json.getAsJsonObject().get("exitCode").getAsInt();
			ProductInfo[] products = context.deserialize(json.getAsJsonObject().get("result"), ProductInfo[].class);
			return new ProductList(exitCode, products);
		}
	};
}